// HtmlPage.cpp: implementation of the HtmlPage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "HtmlPage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

HtmlPage::~HtmlPage()
{
	MyFeatures.clear();
}


bool HtmlPage::GenerateTermsList(char* stream , KeyPhraseCollection &objKeyPhraseCollection)
{
	bool allWentWell = true ;	
	int LanguageID = 0;
	int OutCharCodeID = 0;
	double DesiredNumber = 30;
	void *StopMemory;
	void *DocumentMemory;
	int PhraseListSize;
	int PhraseIndex;
	char *Phrase;
	double Score;
	int ErrorCode;
	char *ErrorMessage;	
	string strPhrase;
	int  Result=0;
	FeatureItem objFeatureItem;

	ErrorCode = ExtrCreateStopMemory(&StopMemory);
	if (ErrorCode > 0)
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);
		return false ;
	}
	ErrorCode = ExtrCreateDocumentMemory(&DocumentMemory);
	if (ErrorCode > 0)
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);	       
		return false ;
	}
	ErrorCode = ExtrActivateHTMLFilter(DocumentMemory, StopMemory);
    if (ErrorCode > 0) 
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);	      
		return false ;
    }
	ErrorCode = ExtrSetNumberPhrases(DocumentMemory, DesiredNumber);
	if (ErrorCode > 0) 
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);		    	
		return false ;
	}
    OutCharCodeID = ENC_DOS;
    ErrorCode = ExtrSetOutputCode(DocumentMemory, OutCharCodeID);
    if (ErrorCode > 0)
	{
        ExtrGetErrorMessage(ErrorCode, &ErrorMessage);          	
        return false ;
    }	
   	ErrorCode = ExtrReadDocumentBuffer(DocumentMemory, StopMemory,
                        (void *) stream, strlen(stream));
   	if (ErrorCode > 0) 
	{
   		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);   		
   		return false ;
   	}
	ErrorCode = ExtrSignalDocumentEnd(DocumentMemory, StopMemory);
	if (ErrorCode > 0) 
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);	      	
		return false ;
	}
	ErrorCode = ExtrGetDocumentLanguage(DocumentMemory, &LanguageID);
	if (ErrorCode > 0) 
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);	       
		return false ;
	}
	ErrorCode = ExtrGetPhraseListSize(DocumentMemory, &PhraseListSize);
	if (ErrorCode > 0) 
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);		
		return false ;
	}
	if (PhraseListSize > 0) 
	{
	    for (PhraseIndex = 0; PhraseIndex < PhraseListSize; PhraseIndex++) 
		{
			ErrorCode = ExtrGetPhraseByIndex(DocumentMemory, PhraseIndex,(void **) &Phrase);
			strPhrase.assign(Phrase);			 
			if (ErrorCode > 0) 
			{
				ExtrGetErrorMessage(ErrorCode, &ErrorMessage);				
				return false ;
			}
			ErrorCode = ExtrGetScoreByIndex(DocumentMemory, PhraseIndex, &Score);
			if (ErrorCode > 0)
			{
				ExtrGetErrorMessage(ErrorCode, &ErrorMessage);		       		
				return false ;
			}
			Result=objKeyPhraseCollection.AddKeyPhraseItem( Phrase );
			objFeatureItem.SetIndex( Result );
			objFeatureItem.SetWeight( Score );
			objFeatureItem.SetFrequency( FindFrequency( Phrase , stream ) );
			MyFeatures.push_back( objFeatureItem );
			
		}
	}
	ErrorCode = ExtrClearDocumentMemory( DocumentMemory );
	if (ErrorCode > 0)
	{
		ExtrGetErrorMessage( ErrorCode, &ErrorMessage );	       
		return false ;
	}
	ErrorCode = ExtrClearStopMemory(StopMemory);
	if (ErrorCode > 0)
	{
		ExtrGetErrorMessage(ErrorCode, &ErrorMessage);	       
		return false ;
	}
	return allWentWell ;
}

int HtmlPage::FindFrequency(string KeyPhrase , char *stream)
{
	CString CStream( stream  );
	CString CKey( KeyPhrase.c_str()  );
	bool Until = true;
	CStream.MakeLower();
	CKey.MakeLower();
	int position=-1,Freq=0;
	while( Until )
	{
		position=CStream.Find( CKey , position + 1 );
		if ( position > -1 )
			Freq++;
		else
			Until = false;
	}
	return Freq;	
}
